package io.github.simplejy.miodemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import io.github.simplejy.miodemo.R;
import io.github.simplejy.miodemo.adapter.UserApplicationAdapter;
import io.github.simplejy.miodemo.bean.UserApplicationData;

/**
 * 选择程序
 */

public class SelectApplicationActivity extends AppCompatActivity {

    private ListView list;
    private ArrayList<UserApplicationData> datas;
    private UserApplicationAdapter adapter;
    private TextView tvSwitch;
    //默认显示程序名
    private boolean isDisplayApplicationName = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_application);

        initView();
    }

    private void initView() {

        list = (ListView) findViewById(R.id.list_application);
        tvSwitch = (TextView) findViewById(R.id.tv_switch_application_diaplay);
        tvSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isDisplayApplicationName) {
                    isDisplayApplicationName = false;
                    tvSwitch.setText(getResources().getString(R.string.application_name));
                } else {
                    isDisplayApplicationName = true;
                    tvSwitch.setText(getResources().getString(R.string.application_id));
                }
                adapter.switchDisplay();
            }
        });
        findViewById(R.id.layout_back_main).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setData();
        adapter = new UserApplicationAdapter(this, datas);
        list.setAdapter(adapter);
    }

    private void setData() {
        datas = new ArrayList<>();

        for (int i = 0; i < 50; i++) {
            UserApplicationData data = new UserApplicationData();
            if (i % 10 == 5) {
                data.setSelected(true);
            }
            data.setApplicationTitle("name" + String.valueOf(i));
            data.setApplicationPackageName("package" + String.valueOf(i));

            datas.add(data);
        }

    }
}
